package com.example.wendao.viewmodel;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.ViewModel;
import androidx.paging.PagedList;

import com.example.wendao.common.AsyncExecutor;
import com.example.wendao.common.PageUtils;
import com.example.wendao.model.entity.NewsComment;
import com.example.wendao.model.service.NewsCommentService;

/**
 * 新闻和论坛的评论ViewModel
 */
public class CommentViewModel extends ViewModel {

    private NewsCommentService newsCommentService;

    {
        newsCommentService = new NewsCommentService();
    }

    /**
     * 添加一条新闻评论
     * @param newsComments 要增加的新闻评论序列
     */
    public void addForum(NewsComment... newsComments){
        new AsyncExecutor<>().execute(() -> newsCommentService.insertForum(newsComments));
    }

    /**
     *
     * @return 分页所有新闻评论列表的分页列表
     */
    public LiveData<PagedList<NewsComment>> findByAllNewsComments(){
        return new PageUtils<NewsComment>()
                .buildLivePagedList(newsCommentService.findByAllNewsComments());
    }

    /**
     *
     * @param fatherid
     * @param newsorforum
     * @return
     */
    public LiveData<PagedList<NewsComment>> findAllNewsCommentsByFatherId(int fatherid, int newsorforum) {
        return new PageUtils<NewsComment>()
                .buildLivePagedList(newsCommentService.findAllNewsCommentsByFatherId(fatherid, newsorforum));
    }

    /**
     *
     * @param fathercommentid
     * @param newsorforum
     * @return
     */
    public LiveData<PagedList<NewsComment>> findAllNewsCommentsByFatherCommentId(int fathercommentid,int newsorforum){
        return new PageUtils<NewsComment>().buildLivePagedList(newsCommentService.findAllNewsCommentsByFatherCommentId(fathercommentid, newsorforum));
    }

    public LiveData<PagedList<NewsComment>> findAllNewsCommentsBynewsids(String sql){
            return new PageUtils<NewsComment>().buildLivePagedList(newsCommentService.findAllNewsCommentsBynewsids(sql));
    }

    public LiveData<PagedList<NewsComment>> findAllForumsCommentsBynewsids(String sql){
        return new PageUtils<NewsComment>().buildLivePagedList(newsCommentService.findAllForumsCommentsBynewsids(sql));
    }

    /**
     *
     * @param newsComments  要更新的评论
     */
    public void updateNewsComments(NewsComment... newsComments){
        new AsyncExecutor<>().execute(() -> newsCommentService.updateNewsComments(newsComments));
    }

    /**
     *
     * @param newsComments  要删除的评论
     */
    public void deleteNewsComment(NewsComment... newsComments) {
        new AsyncExecutor<>().execute(() -> newsCommentService.deleteNewsComment(newsComments));
    }

}

